package com.example.expinterview.config;

import org.apache.commons.io.IOUtils;

import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.*;

public class WrappedHttpServletRequest extends HttpServletRequestWrapper {

  private final byte[] bytes;
  private final WrappedServletInputStream wrappedServletInputStream;

  public WrappedHttpServletRequest(HttpServletRequest request) throws IOException {
    super(request);
    // 读取输入流里的请求参数，并保存到bytes里
    bytes = IOUtils.toByteArray(request.getInputStream());
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
    this.wrappedServletInputStream = new WrappedServletInputStream(byteArrayInputStream);

    // 很重要，把post参数重新写入请求流
    reWriteInputStream();
  }

  /**
   * 把参数重新写进请求里
   */
  public void reWriteInputStream() throws IOException {
    wrappedServletInputStream
        .setStream(new ByteArrayInputStream(bytes != null ? bytes : new byte[0]));
  }

  @Override
  public ServletInputStream getInputStream() throws IOException {
    return wrappedServletInputStream;
  }

  @Override
  public BufferedReader getReader() throws IOException {
    return new BufferedReader(new InputStreamReader(wrappedServletInputStream));
  }

  /**
   * 获取post参数，可以自己再转为相应格式
   */
  public String getRequestParams() throws IOException {
    return new String(bytes, this.getCharacterEncoding());
  }

  /**
   * 内部类
   */
  private static class WrappedServletInputStream extends ServletInputStream {


    private InputStream stream;

    public WrappedServletInputStream(InputStream stream) {
      this.stream = stream;
    }

    public void setStream(InputStream stream) {
      this.stream = stream;
    }

    @Override
    public int read() throws IOException {
      return stream.read();
    }

    @Override
    public boolean isFinished() {
      return true;
    }

    @Override
    public boolean isReady() {
      return true;
    }

    @Override
    public void setReadListener(ReadListener readListener) {}
  }
}